﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using GWEB.WebBase.Data;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;
using Newtonsoft.Json;

namespace GWEB.WebFrame.Sys
{
    public class CondController : PageInfoBase
    {        
        [ActionFilter(IsLogin = true)]
        public ActionResult Index(String id, String keyCode)
        {
            String sqlUser = String.Format("select iuserid as [key],sshowname as value from tb_sys_users  order by [key]");
            String sqlRole = String.Format("select srolecode as [key],srolename as value from tb_sys_role order by [key]");
            String sqlCondExp = String.Format("select id,sfieldname,soperator,svalue,slogic,case when slogic='and' then '与' when slogic='or' then '或' end slogic_cn from tb_sys_cond_exp where icondid={0} order by id", id);

            //BaseData bd=new BaseData();
            DataTable dtUser = BasePage.bd.ExecuteTable(sqlUser);
            DataTable dtRole = BasePage.bd.ExecuteTable(sqlRole);
            DataTable dtCondExp = BasePage.bd.ExecuteTable(sqlCondExp);
           
            ViewBag.dtUser=dtUser;
            ViewBag.dtRole=dtRole;
            ViewBag.dtCondExp=dtCondExp;            
            return Load("Cond", "Sys/Cond", id, keyCode); 
        }
        protected override SqlState SqlExecute(BaseData bd, String id, Dictionary<String, Object> data)
        {
            String rowData = data["rowData"].ToString();
            String rowDataDel = data["rowDataDel"].ToString();
            data.Remove("rowData");
            data.Remove("rowDataDel");
            //多行表插入更新           
            List<Dictionary<String, Object>> dt = new List<Dictionary<String, Object>>();
            dt=JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(rowData);
            String[] sql = new String[dt.Count + 2];
            sql[0] = BaseData.MapToSql("tb_sys_cond", "id", id, data);
            if (!rowDataDel.Equals("")) sql[1] = String.Format("delete tb_sys_cond_exp where id in ({0})", rowDataDel);//删除
            String[] sqlExp = BaseData.ListToSql("tb_sys_cond_exp", "id", "icondid", dt);
            int i = 2;
            foreach (String s in sqlExp) sql[i++] = s;
            bool t = bd.ExecuteSQL(sql, -1, "tb_sys_cond", "icondid", id);
            return t ? SqlState.True : SqlState.False;
        }

    }
}